import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as express from 'express';
import axios from 'axios';
import { Db, MongoClient, ObjectId } from "mongodb";
import { writeFile } from "fs";
// import { exec } from 'child_process';

// writeFile('/root/vol/example.txt', '这是一个示例文件', (err) => {
//   if (err) throw err;
//   console.log('文件已创建');
// });
// let i = 0;
// console.log("#####start#####")
// exec(`apt-get update && apt-get install wget`, (error, stdout, stderr) => {
//     ++i;
//     if (error) {
//         console.error(i + `执行错误： ${error}`);
//         return;
//     }
//     console.log(i + `stdout: ${stdout}`);
//     console.error(i + `stderr: ${stderr}`);
// });


async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    app.use('/static', express.static('/root/vol'));
    app.enableCors();
    await app.listen(3000);
}
bootstrap();



// const url = 'https://video.twimg.com/ext_tw_video/1494279082082775048/pu/vid/324x270/aMvpiOaEAMlIvlmb.mp4?tag=12';
// const outputFileName = '/root/vol/kkkkk.mp4';

// exec(`wget ${url} -O ${outputFileName}`, (error, stdout, stderr) => {
//     if (error) {
//         console.error(`执行错误： ${error}`);
//         return;
//     }
//     console.log(`stdout: ${stdout}`);
//     console.error(`stderr: ${stderr}`);
// });



// let url = 'https://www8.garmin.com/manuals-apac/webhelp/forerunner245245music/ZH-TW/static/images/temp/logo.png';
// download(url);

async function download(url) {
    try {
        const res = await axios.get(url, { responseType: 'arraybuffer' });
        const name = Date.now() + ".jpg";
        console.log("name", name)
        writeFile(name, res.data, err => {
            if (err) {
                console.error(err);
            }
        });
        console.log("download over")
        return name;
    }
    catch (e) {
        console.log(e);
        return "0";
    }

}

async function link() {
    const link = "mongodb://admin:jsz04005301@120.48.63.39:27017";
    const client = new MongoClient(link);
    await client.connect();
    console.log("Connected successfully to server!");
    const db = client.db("book");
    const collection = db.collection("video");
    const temp = await collection.find({ img: { $exists: false } }).toArray();
    const total = temp.length;
    console.log("total", total);
    let i = 0;

    do {
        const obj = await collection.find({ img: { $exists: false } }).limit(1).toArray();
        console.log(i, "start");
        console.log(obj);

        if (obj.length) {
            const name = await download(obj[0].poster);
            console.log(name);

            await collection.updateOne({ _id: new ObjectId(obj[0]._id) }, { $unset: { pic: "" }, $set: { img: name } });
            // console.log("2 type of ", typeof res);
            // console.log(JSON.stringify(res));
            console.log(i + "/" + total + " ---- over")
        }
        else {
            break;
        }
    } while (++i <= total)

}


